package com.liangtian.mdm.system.code.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.dm.config.dao.AttrConfigDao;
import com.liangtian.mdm.system.code.dao.CodeDao;
import com.liangtian.mdm.system.code.model.Code;
import com.liangtian.mdm.system.code.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CodeServiceImp implements CodeService {

    //注入dao
    @Autowired
    CodeDao codeDao;

    @Autowired
    AttrConfigDao attrConfigDao;

    @Override
    public List<Code> queryFieldCodes(Map<String, Object> param) {
        return codeDao.selectCodeConfigs(param);
    }

    @Override
    public boolean insertConfigCodes(List<Code> codeLst) {
        boolean success = false;
        try {
            codeDao.insertConfigCodes(codeLst);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }

    @Override
    public boolean deleteConfigCodes(String attrId) {
        boolean success = false;
        try {
            codeDao.deleteConfigCodesByAttrId(attrId);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }

    @Override
    public List<Code> addCodeList(Map<String, Object> param) {
        List<Code> codeLst = new ArrayList<Code>();
        String codeRuleIndex = (String) param.get("codeRuleIndex");
        String[] codeRuleIndexArray = codeRuleIndex.split("[+]");
        String codeRuleIndexArrayItem;
        String codeRuleIndexArrayItemPrefix;
        String codeRuleIndexArrayItemSuffix;
        for (int i = 0; i < codeRuleIndexArray.length; i++) {
            Code code = new Code();
            code.setAttrId((String) param.get("attrId"));
            code.setSurveyType((String) param.get("modelId"));
            codeRuleIndexArrayItem = codeRuleIndexArray[i];
            int charIndex = codeRuleIndexArrayItem.lastIndexOf("e");
            codeRuleIndexArrayItemPrefix = codeRuleIndexArrayItem.substring(0, charIndex + 1);
            code.setCodeType(codeRuleIndexArrayItemPrefix);
            codeRuleIndexArrayItemSuffix = codeRuleIndexArrayItem.substring(charIndex + 1);
            if ("hyCode".equals(codeRuleIndexArrayItemPrefix)) {
                String hyCodeValue = (String) param.get("hyCode" + codeRuleIndexArrayItemSuffix + "");
                //String[] hyCodeValueArray = hyCodeValue.split("--");
                //code.setCodeValue(hyCodeValueArray[1]);
                code.setCodeValue(hyCodeValue);
                code.setNameValue((String) param.get("hyCodeName" + codeRuleIndexArrayItemSuffix + ""));
                code.setConfigCodeField((String) param.get("hyConfigCode" + codeRuleIndexArrayItemSuffix + ""));
                code.setConfigCodeName((String) param.get("hyConfigCodeName" + codeRuleIndexArrayItemSuffix + ""));
                code.setValueLength((String) param.get("hyLength" + codeRuleIndexArrayItemSuffix + ""));
                codeLst.add(code);
            } else if ("sjCode".equals(codeRuleIndexArrayItemPrefix)) {
                code.setValueLength((String) param.get("sjLength" + codeRuleIndexArrayItemSuffix + ""));
                codeLst.add(code);
            } else if ("lsCode".equals(codeRuleIndexArrayItemPrefix)) {
                code.setValueLength((String) param.get("lsLength" + codeRuleIndexArrayItemSuffix + ""));
                code.setStartValue((String) param.get("startValue" + codeRuleIndexArrayItemSuffix + ""));
                code.setIncrementValue((String) param.get("incrementValue" + codeRuleIndexArrayItemSuffix + ""));
                codeLst.add(code);
            } else if ("dateCode".equals(codeRuleIndexArrayItemPrefix)) {
                String dateValue = (String) param.get("dateCode" + codeRuleIndexArrayItemSuffix + "");
                code.setValueType(dateValue);
                if ("sysdate".equals(dateValue)) {
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String dateFormatValue = sdf.format(date);
                    code.setCodeValue(dateFormatValue);
                } else
                    code.setCodeValue((String) param.get("fixedDateCode" + codeRuleIndexArrayItemSuffix + ""));
                codeLst.add(code);
            } else if ("timeCode".equals(codeRuleIndexArrayItemPrefix)) {
                String timeValue = (String) param.get("timeCode" + codeRuleIndexArrayItemSuffix + "");
                code.setValueType(timeValue);
                if ("systime".equals(timeValue)) {
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                    String timeFormatValue = sdf.format(date);
                    code.setCodeValue(timeFormatValue);
                } else
                    code.setCodeValue((String) param.get("fixedTimeCode" + codeRuleIndexArrayItemSuffix + ""));
                codeLst.add(code);
            } else if ("fixCode".equals(codeRuleIndexArrayItemPrefix)) {
                code.setCodeValue((String) param.get("fixedCode" + codeRuleIndexArrayItemSuffix + ""));
                codeLst.add(code);
            }

        }
        return codeLst;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo queryCodeAttrs(Map<String, Object> param) {
        PageInfo<List<Map<String, Object>>> pageInfo = null;
        try {
            Object pNo = param.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());//页码
            } else {
                pageNum = 1;
            }
            //pageSize=0时不分页
            pageSize = Integer.parseInt(param.get("limit").toString());//每页条数
            PageHelper.startPage(pageNum, pageSize);//分页  注释掉后就不会进行分页
            List<Map<String, Object>> list = attrConfigDao.queryAllConfigCodeAttrs(param);
            pageInfo = new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public boolean deleteConfigCodesByIds(Map<String, Object> param) {
        boolean success = false;
        try {
            codeDao.deleteConfigCodesByAttrIdLst(param);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }


}
